<?php

/**
 * This is the model class for table "facturas".
 *
 * The followings are the available columns in table 'facturas':
 * @property string $IDFACTURA
 * @property string $FECHA
 * @property string $HORA
 * @property string $IDUSUARIO
 * @property string $IDCL
 * @property integer $TIPO
 *
 * The followings are the available model relations:
 * @property Detfactura[] $detfacturas
 * @property Clientes $iDCL
 * @property Usuario $iDUSUARIO
 */
class Facturas extends CActiveRecord
{
	public $cliente;
	public $usuario;
	public $apellido;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Facturas the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'facturas';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('IDFACTURA, FECHA, IDCL, TIPO', 'required'),
			array('TIPO', 'numerical', 'integerOnly'=>true),
			array('IDFACTURA', 'length', 'max'=>10),
			array('IDUSUARIO, IDCL', 'length', 'max'=>20),
			array('IDFACTURA','unique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('IDFACTURA, FECHA, IDUSUARIO, IDCL, TIPO, usuario, cliente, apellido', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'detfacturas' => array(self::HAS_MANY, 'Detfactura', 'IDFACTURA'),
			'iDCL' => array(self::BELONGS_TO, 'Clientes', 'IDCL'),
			'iDUSUARIO' => array(self::BELONGS_TO, 'Usuario', 'IDUSUARIO'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'IDFACTURA' => '# Factura',
			'FECHA' => 'Fecha',
			'IDUSUARIO' => 'Id Usuario',
			'IDCL' => 'Ced. Cliente',
			'TIPO' => 'Tipo Factura',
			'usuario' => 'Nombre de Usuario',
			'cliente'=>'Nombre de Cliente',
			'apellido'=>'Apellido de Cliente',

		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		$criteria->with = array('iDCL','iDUSUARIO');
        $criteria->addSearchCondition('iDCL.NOMBRE', $this->cliente);
		$criteria->addSearchCondition('iDUSUARIO.NOMBRE', $this->usuario);
		$criteria->addSearchCondition('iDUSUARIO.APELLIDO', $this->apellido);
		
		$criteria->compare('IDFACTURA',$this->IDFACTURA,true);
		$criteria->compare('FECHA',$this->FECHA,true);
		$criteria->compare('IDUSUARIO',$this->IDUSUARIO,true);
		$criteria->compare('IDCL',$this->IDCL,true);
		$criteria->compare('TIPO',$this->TIPO,true);

		$_SESSION['datos'] = new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
                'pagination'=>false,
        ));


		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

public $minAge ='2012-01-01';
public $maxAge = '2013-08-01';
	public function fechas()
	{

		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('IDFACTURA',$this->IDFACTURA,true);
		//$criteria->addBetweenCondition('t.date_add','2012-02-08','2012-02-14');
		//$criteria->condition = "FECHA BETWEEN '$a' AND '$b'";
		$criteria->addBetweenCondition('FECHA', $this->minAge, $this->maxAge);

		$criteria->compare('IDUSUARIO',$this->IDUSUARIO,true);
		$criteria->compare('IDCL',$this->IDCL,true);
		$criteria->compare('TIPO',$this->TIPO,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

}